/*
 * The Open Source Unix Installer
 *
 * The MIT License
 *
 * Copyright 2011-2012 Andrey Pudov.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * @author  Andrey Pudov    <andrey@andreypudov.com>
 * @version 0.00.00
 * @name    u_system.h
 * @date:   Feb 14, 2012
 */

#ifndef INSTALLER_PROC_U_SYSTEM_H_
#define INSTALLER_PROC_U_SYSTEM_H_

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "system.h"

#include "u_main.h"

#ifdef __cplusplus
extern "C" {
#endif

bool u_installer_system_start();                 /* unit entry point */

bool u_installer_system_get_system_name();
bool u_installer_system_get_node_name();
bool u_installer_system_get_os_release();
bool u_installer_system_get_version();
bool u_installer_system_get_machine();

bool u_installer_system_start() {
    return (u_installer_system_get_system_name()
        && u_installer_system_get_node_name()
        && u_installer_system_get_os_release()
        && u_installer_system_get_version() 
        && u_installer_system_get_machine());
}

/* creates and compares two strings by invoking uname and functions to test */
bool u_installer_system_get_system_name() {
    /* open the command for reading */
    FILE *process_pipe = popen("uname -a", "r");

    /* command output */
    char buffer[256];
    /* value to compare */
    char unit_value[256];

    char *system_name = installer_system_get_system_name();
    char *node_name   = installer_system_get_node_name();
    char *release     = installer_system_get_os_release();
    char *version     = installer_system_get_version();
    char *machine     = installer_system_get_machine();

    if (process_pipe == NULL) {
        u_installer_main_print("system", "get_system_name()", false);
        
        return false;
    }

    /* read the output a line at a time */
    while (fgets(buffer, sizeof buffer, process_pipe) != NULL) {
        /* reading loop */
    }

    /* close */
    pclose(process_pipe);

    
    /* build unit value */
    snprintf(unit_value, sizeof unit_value, "%s %s %s %s %s", 
             system_name, node_name, release, version, machine);

    /* free allocated memmory */
    free(system_name);
    free(node_name);
    free(release);
    free(version);
    free(machine);

    /* compare 'uname -a' output and generated string */
    if (strncmp(buffer, unit_value, strnlen(unit_value, sizeof unit_value)) == 0) {
        u_installer_main_print("system", "get_system_name()", true);

        return true;
    } else {
        u_installer_main_print("system", "get_system_name()", false);

        return false;
    }
}

bool u_installer_system_get_node_name() {
    /* unit case is a part of get_system_name() */
    u_installer_main_print("system", "get_node_name()", true);

    return true;
}

bool u_installer_system_get_os_release() {
    /* unit case is a part of get_system_name() */
    u_installer_main_print("system", "get_os_release()", true);

    return true;
}

bool u_installer_system_get_version() {
    /* unit case is a part of get_system_name() */
    u_installer_main_print("system", "get_version()", true);

    return true;
}

bool u_installer_system_get_machine() {
    /* unit case is a part of get() */
    u_installer_main_print("system", "machine()", true);

    return true;

}

#ifdef __cplusplus
}
#endif

#endif /* INSTALLER_PROC_U_SYSTEM_H_ */ 
